package com.pda.gongpin.ui.manualhandling;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;

import com.pda.gongpin.R;
import com.pda.gongpin.bean.ProductModelBean;
import com.pda.gongpin.bean.RawMaterialTypeBean;
import com.pda.gongpin.bean.SelectBean;
import com.pda.gongpin.bean.StorageBinIdBean;
import com.pda.gongpin.bean.WareAreaBean;
import com.pda.gongpin.bean.request.ManualHandRequest;
import com.pda.gongpin.bean.request.StorageBinQueryRequest;
import com.pda.gongpin.contract.ManualHandlingContract;
import com.pda.gongpin.presenter.ManualHandlingPresenter;
import com.pda.gongpin.utils.PopUtisl;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.base.util.SharedPreferencesUtil;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = AppActivityKey.MANUALHANDLINGACTIVITY)
public class ManualHandlingActivity extends BaseMvpActivity<ManualHandlingPresenter> implements ManualHandlingContract.View {
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.tv_start1)
    TextView tvStart1;
    @BindView(R.id.tv_end1)
    TextView tvEnd1;
    @BindView(R.id.tv_start2)
    TextView tvStart2;
    @BindView(R.id.tv_end2)
    TextView tvEnd2;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;

    private List<SelectBean> rawMaterialTypeBeans=new ArrayList<>();


    @Override
    public ManualHandlingPresenter getPresenter() {
        return new ManualHandlingPresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_manual_handling;
    }

    @Override
    public void initView() {
        presenter.getSelectAreaAndStorage();
    }

    @Override
    public void initLinsenterner() {

    }

    @Override
    public void getData() {

    }

    @Override
    public void setAreaList(List<WareAreaBean> list) {
        rawMaterialTypeBeans.clear();
        for (WareAreaBean wareAreaBean: list){
            rawMaterialTypeBeans.add(new SelectBean(wareAreaBean.getWareAreaName(),wareAreaBean.getWareAreaId()));
        }

    }

    @Override
    public void setStorageBinList(List<StorageBinIdBean> list, boolean isto) {
        List<SelectBean> selectBeans=new ArrayList<>();
        for (StorageBinIdBean wareAreaBean: list){
            selectBeans.add(new SelectBean(wareAreaBean.getStorageBinName(),wareAreaBean.getStorageBinId()));
        }
        if (isto){
              PopUtisl.showSelectPop(this, selectBeans, tvEnd2, new PopUtisl.SelectClick() {
                    @Override
                    public void onClick(SelectBean bean) {
                        tvEnd2.setText(bean.getName());
                        endbean2 = bean;
                    }
                }, "选择终点库存");
        }else {
            PopUtisl.showSelectPop(this, selectBeans, tvEnd1, new PopUtisl.SelectClick() {
                @Override
                public void onClick(SelectBean bean) {
                    tvEnd1.setText(bean.getName());
                    endbean1 = bean;
                }
            }, "选择起点库存");
        }
    }

    @Override
    public void onSuccess() {
        tvStart1.setText("");
        tvStart2.setText("");
        tvEnd1.setText("");
        tvEnd2.setText("");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    SelectBean startbean1, startbean21;
    SelectBean endbean1, endbean2;

    @OnClick({R.id.tv_start1, R.id.tv_end1, R.id.tv_start2, R.id.tv_end2, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_start1:
                if (rawMaterialTypeBeans == null) {
                    presenter.getSelectAreaAndStorage();
                    return;
                }
                PopUtisl.showSelectPop(this, rawMaterialTypeBeans, tvStart2, new PopUtisl.SelectClick() {
                    @Override
                    public void onClick(SelectBean bean) {
                        if (startbean21 != null) {
                            if (bean.getId().equals(startbean21.getId())) {
                                showMessage("起点和终点不能相同！");
                                return;
                            }
                            tvStart1.setText(bean.getName());
                            startbean1 = bean;
                        } else {
                            tvStart1.setText(bean.getName());
                            startbean1 = bean;
                        }
                    }
                }, "选择起点区域");
                break;
            case R.id.tv_end1:
                if (startbean1 == null) {
                    showMessage("请先选择起点区域");
                    return;
                }
                StorageBinQueryRequest request=new StorageBinQueryRequest();
                request.setWarehouseId(startbean1.getId());
                presenter.storageBinQuery(request,false);
                break;
            case R.id.tv_start2:
                if (rawMaterialTypeBeans == null) {
                    presenter.getSelectAreaAndStorage();
                    return;
                }
                PopUtisl.showSelectPop(this, rawMaterialTypeBeans, tvStart2, new PopUtisl.SelectClick() {
                    @Override
                    public void onClick(SelectBean bean) {
                        if (startbean1 != null) {
                            if (bean.getId().equals(startbean1.getId())) {
                                showMessage("起点和终点不能相同！");
                                return;
                            }
                            tvStart2.setText(bean.getName());
                            startbean21 = bean;
                        } else {
                            tvStart2.setText(bean.getName());
                            startbean21 = bean;
                        }
                    }
                }, "选择终点区域");
                break;
            case R.id.tv_end2:
                if (startbean21 == null) {
                    showMessage("请先选择终点区域");
                    return;
                }
                StorageBinQueryRequest request1=new StorageBinQueryRequest();
                request1.setWarehouseId(startbean21.getId());
                presenter.storageBinQuery(request1,true);
                break;
            case R.id.tv_confirm:
                if (startbean1 == null) {
                    showMessage("起点区域不能为空！");
                    return;
                }
                if (endbean1 == null) {
                    showMessage("起点库存不能为空！");
                    return;
                }
                if (startbean21 == null) {
                    showMessage("终点区域不能为空！");
                    return;
                }
                if (endbean2 == null) {
                    showMessage("终点库存不能为空！");
                    return;
                }
                ManualHandRequest handRequest = new ManualHandRequest();
                handRequest.setSrcWareAreaId(startbean1.getId());
                handRequest.setSrcStorageBinId(endbean1.getId());
                handRequest.setDestWareAreaId(startbean21.getId());
                handRequest.setDestStorageBinId(endbean2.getId());
                presenter.handiworkOutbound(handRequest);
                break;
        }
    }

    @OnClick({R.id.iv_close, R.id.tv_back})
    public void onViewClicked1(View view) {
        finishActivity();
        switch (view.getId()) {
            case R.id.iv_close:
                break;
            case R.id.tv_back:
                break;
        }
    }

}
